import { Loading, Message } from "element-ui";
function openLoading() {
  return Loading.service({
    lock: true,
    text: "Loading",
    spinner: "el-icon-loading",
    background: "rgba(0, 0, 0, 0.7)",
  });
}
async function get(url = "", id) {
  const loading = openLoading();
  try {
    url = `${this.baseUrl}${url}`;
    if (typeof id == "object") {
      let args = "?";
      for (const key in id) {
        if (Object.hasOwnProperty.call(id, key)) {
          if (id[key]) {
            args += `${key}=${id[key]}&`;
          }
        }
      }
      args = args.substring(0, args.length - 1);
      url += args;
    } else {
      url += `${id ? "/" + id : ""}`;
    }
    console.log(url);
    let response = await fetch(url, {
      headers: {
        Authorization: window.sessionStorage.getItem("token"),
      },
    });

    if (response.ok) {
      response.data = await response.json();
    }
    return response;
  } catch (err) {
    console.error(err);
    Message.error("请求失败,服务器无响应");
  } finally {
    loading.close();
  }
}
async function post(url = "", data) {
  const loading = openLoading();
  try {
    let response = await fetch(`${this.baseUrl}${url}`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        Authorization: window.sessionStorage.getItem("token"),
      },
      body: JSON.stringify(data),
    });
    if (response.ok) {
      response.data = await response.json();
    }
    return response;
  } catch (err) {
    Message.error("请求失败,服务器无响应");
    throw err;
  } finally {
    loading.close();
  }
}

async function put(url = "", id, item) {
  if (!id) {
    throw "id 为空";
  }
  // if (typeof id != "number") {
  //   throw `typeof id is ${typeof id}`;
  // }

  if (!item) {
    throw `item is ${item}`;
  }
  const loading = openLoading();
  url = `${this.baseUrl}${url}${id ? "/" + id : ""}`;
  try {
    let response = await fetch(url, {
      method: "PUT",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: window.sessionStorage.getItem("token"),
      },
      body: JSON.stringify(item),
    });
    if (response.ok) {
      return response;
    }
    throw response;
  } catch (err) {
    console.error(err);
    Message.error("请求失败,服务器无响应");
    throw err;
  } finally {
    loading.close();
  }
}

async function patch(url = "", id, item) {
  if (!id) {
    throw "id 为空";
  }
  // if (typeof id != "number") {
  //   throw `typeof id is ${typeof id}`;
  // }

  if (!item) {
    throw `item is ${item}`;
  }
  const loading = openLoading();
  url = `${this.baseUrl}${url}${id ? "/" + id : ""}`;
  try {
    let response = await fetch(url, {
      method: "PATCH",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: window.sessionStorage.getItem("token"),
      },
      body: JSON.stringify(item),
    });

    if (response.ok) {
      return response;
    }
  } catch (err) {
    console.error(err);
    Message.error("请求失败,服务器无响应");
  } finally {
    loading.close();
  }
}

async function Delete(url = "", id) {
  if (!id) {
    throw `id is${id} `;
  }
  if (typeof id == "object") {
    throw "id is object";
  }
  const loading = openLoading();
  url = `${this.baseUrl}${url}${id ? "/" + id : ""}`;
  try {
    let response = await fetch(url, {
      method: "DELETE",
      headers: {
        Accept: "application/json",
        "Content-Type": "application/json",
        Authorization: window.sessionStorage.getItem("token"),
      },
    });
    if (response.ok) {
      return response;
    }
    throw response;
  } catch (err) {
    console.error(err);
    Message.error("请求失败,服务器无响应");
    throw err;
  } finally {
    loading.close();
  }
}
const shios = {
  token: "",
  baseUrl: "",
  get,
  post,
  put,
  patch,
  Delete,
};

export default shios;
